<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends AdminController {
   
    private $_userInfo;//操作用户信息的表
    private $_user;//操作用户表
    private $_photo; //操作相册对像
    private $_model; //操作model类
    public   function __construct(){
        parent::__construct();
        $this->_user=M('user');
        $this->_userInfo=M('userinfo');
        $this->_photo=M('userphoto');
        $this->_model=M();
    }
    public function index(){

        // $dataList=
        // 
        $id=$_SESSION['love_user']['id'];
        // var_dump($_SESSION);
         //模拟session
        $sql="select s_city,sex from love_userinfo where uid={$id}";
        $dataCity=$this->_userInfo->query($sql);
        // dump($dataCity);    
        $cityId=(empty($dataCity[0]['s_city'])==1)?6:$dataCity[0]['s_city'];
        $sex=$dataCity[0]['SEX'];
        $tsex=($sex=='1')?0:1; 
        // dump($tsex);
        //1：上半部遍历同城  根据登录用户的男女
        $sql="select u.name uname,u.uid uuid,sex,p.name pname,edu from love_userinfo u,love_userphoto p where u.uid=p.uid and s_city={$cityId} and p.is_face=1 and sex={$tsex} limit 8";

         $dataList1=$this->_model->query($sql);
          dump($this->_model->getLastSql());
         //dump($dataList1);
        //2：下半部遍历不是同城的
        $sqlT="select u.name uname,u.uid uuid,sex,p.name pname,edu from love_userinfo u,love_userphoto p where u.uid=p.uid and s_city!={$cityId} and p.is_face=1 and sex={$tsex} limit 6";
        $dataList2=$this->_model->query($sqlT);
         dump($this->_model->getLastSql());
        $this->assign('dataList1',$dataList1);
        $this->assign('dataList2',$dataList2);
        var_dump($dataList1);
        var_dump($dataList2);
        $this->display();
    }
    public function getData(){
    	// $data=M('adminuser')->select();
    	$data=null;
    	$this->ajaxReturn($data,'json');
    }
    public function UserDetails(){
        $id = I('get.id');
         
         // var_dump($id);
         
        $strname = M('userinfo')->where("uid=$id")->select();
        $strname = $strname[0]; 
        // echo M('userinfo')->getLastSql();
        // var_dump( $strname);
        // 查询地区
        $name = M('user')->where("id=$id")->select();
        dump($name);
        $diqu    = $strname['s_province'];
        // var_dump($diqu);
        $diqu = M('district')->where("id=$diqu")->select();
        $data = M('userphoto')->where("uid=$id")->select();
        $this->assign('name',$name[0]);
        $this->assign('diqu',$diqu[0]);
        $this->assign('list',$strname);
        $this->assign('data',$data);

       // var_dump($_SESSION);
       //  var_dump($name);
       //  var_dump($diqu);
       //  var_dump($strname);
       //  var_dump($data);
    	$this->display();
    }

    public function youjian(){
    
        $eid = I('post.id');
        $type=I('post.type');
        $content = I('post.content');
        $uid = $_SESSION['love_user']['id'];
        $uname = $_SESSION['love_user']['name'];
        $theme ="《".$uname."》 像你打招呼！";
        $time = time();
        $data['uid']=$uid;
        $data['eid']=$eid;
        $data['time']=$time;
        $data['theme']=$theme;
        $data['content']=$content;
        $data['type']=$type;
        $dataList=M('email')->add($data);
       
    }


    public function gift(){
     /*   var_dump($_GET);*/
        $id=$_GET['id'];
        $data = M('gift')->select();
     /*   var_dump($data);*/
        $this->assign('data',$data);
        $this->assign('list',$id);

        $this->display();

    }

    public function deal(){
         /*var_dump($_POST);*/
         $uid=$_SESSION['love_user']['id'];
         $eid = I('post.uid');
         $name = I('post.name');
         /*var_dump($name);*/
         $data = M('gift')->where("name='".$name."'")->find();
     /*    var_dump($data);*/
         $data2 = M('userinfo')->where("uid=$uid")->find();
       /*  var_dump($data2);*/
         $integral = $data2['integral']-$data['value'];
         M('userinfo')->integral = $integral;
        $ral['uid']=$uid;
        $ral['type']=0;
        $ral['value']=$data['value'];
        $ral['reason']='购买礼物';
        $gift['gid']=$data['id'];
        $gift['uid']=$uid;
        $gift['eid']=$eid;
        $gift['gtime']=time();

         if($data['value'] > $data2['integral']){
            $this->error('积分不足情尽快充值！',U('Home/Usercenter/index'));
         }else{
            if(M('userinfo')->where("uid=$uid")->save()>0){
                if(M('integral')->add($ral)>0){
                    /*var_dump($ral);
                    echo M('integral')->getLastSql();
                    exit;*/
                    if(M('dealgift')->add($gift)>0){
                        $this->success('礼物发送成功',U('UserDetails',array('id'=>$eid)));
                    }else{
                        $this->error('礼物发送失败');
                    }
                }else{
                $this->error('礼物发送失败！');  
                }
            }else{
                $this->error('礼物发送失败！');
            }
         }

    }

}